/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sales.managedbean;

import javax.faces.event.ActionEvent;
import java.io.IOException;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import sales.entity.SalesQuotation;
import sales.seesion.stateless.SalesQuotationSessionBean;
import sales.util.exception.QuotationNotFoundException;

/**
 *
 * @author xhx
 */
@ManagedBean
@ViewScoped
public class ConvertSalesQuotationManagedbean {

    private Long quotationId = null;
    @EJB
    SalesQuotationSessionBean salesQuotationSessionBean;

    /** Creates a new instance of ConvertSalesQuotationManagedbean */
    public ConvertSalesQuotationManagedbean() {
    }

    public void redirect(ActionEvent event) throws QuotationNotFoundException, IOException {

        SalesQuotation salesQuotation = salesQuotationSessionBean.getQuotationById(quotationId);

        if (salesQuotation == null) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Invalid Quotation Id, Please Check Again", ""));
            return;
        }
        if (salesQuotation.isUsed()) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "This Quotation is Used", ""));
            return;
        }
        salesQuotation.setUsed(true);
        FacesContext.getCurrentInstance().getExternalContext().getFlash().put("salesQuotation", salesQuotation);
        FacesContext.getCurrentInstance().getExternalContext().redirect("createNewPurchaseOrder.xhtml");
    }

    public Long getQuotationId() {
        return quotationId;
    }

    public void setQuotationId(Long quotationId) {
        this.quotationId = quotationId;
    }
}
